package com.ujs.edu.medicine.controller;

import com.auth0.jwt.JWT;
import com.ujs.edu.medicine.dao.*;
import com.ujs.edu.medicine.dataobject.*;
import com.ujs.edu.medicine.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:
 * @Date: Created in 18:07 2020/9/21
 * Description: 集成搜索
 * Modified By:
 */
@Controller
public class SearchController {

    @Autowired
    private YunjiayiPostMapper yunjiayiPostMapper;
    @Autowired
    private YunjiayiShopMapper yunjiayiShopMapper;
    @Autowired
    private YunjiayiPrescriptionMapper yunjiayiPrescriptionMapper;

    @Autowired
    private HttpServletRequest request;

    public String gettoken(){
        return request.getHeader("Authorization");
    }



    /*
    * 集成搜索
    * */
    @RequestMapping("/api/search")
    @ResponseBody
    public Result searchInfo(@RequestParam("content") String content){

        Result result=new Result();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid=JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您未登陆,请登陆");
            return result;
        }


        List<SearchObj> searchObjs=new ArrayList<>();
        SearchObj searchObj=null;

        if (content.trim().isEmpty()||content==null){
            result.setCode(1);
            result.setMessage("输入内容有错");
            result.setData(searchObjs);
            return result;
        }

        System.out.println(searchObjs.size());
        List<YunjiayiPost>posts=yunjiayiPostMapper.searchLikePost(content);
        System.out.println(posts.size()+"post");
        if (posts.size()!=0){
            for (YunjiayiPost post:posts){
                searchObj=new SearchObj();
                searchObj.setId(post.getPostId());
                searchObj.setType("post");
                searchObj.setAbstracttest(post.getContent());
                searchObjs.add(searchObj);
            }
        }
        System.out.println(searchObjs.size());
        List<YunjiayiShop> shops=yunjiayiShopMapper.selectLike(content);
        if (shops.size()!=0){
            for (YunjiayiShop shop:shops){
                searchObj=new SearchObj();
                searchObj.setId(shop.getShopid());
                searchObj.setType("shop");
                searchObj.setAbstracttest(shop.getDescription());
                searchObjs.add(searchObj);
            }
        }


        System.out.println(searchObjs.size());
        List<YunjiayiPrescription> prescriptions=yunjiayiPrescriptionMapper.searchLikePres(content);
        if (prescriptions.size()!=0){
            System.out.println(prescriptions.size());
            int i=0;
            for(YunjiayiPrescription prescription:prescriptions){
                searchObj=new SearchObj();
                System.out.println(i+1);
                Integer id=prescription.getPrescriptionid();
                String pres=prescription.getDecription();
                searchObj.setId(7);
                searchObj.setType("prescription");
                searchObj.setAbstracttest(pres.trim());
                searchObjs.add(searchObj);
            }
        }

        if (searchObjs!=null){
            result.setCode(0);
            result.setMessage("查询成功");
            result.setData(searchObjs);
        }else {
            result.setCode(1);
            result.setMessage("查询失败");
        }
        return result;
    }

}
